package cn.gzsxt.dao.impl;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;

import cn.gzsxt.dao.MyBaseDao;
import cn.gzsxt.po.TaskDetail;
import cn.gzsxt.utils.DBUtil;

public class MyBaseDaoImpl implements MyBaseDao {

	public <T> List getSelectRows(Class<T> clz, String sql, Object... params) {
		List<T> list = new ArrayList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++){
				ps.setObject(1+1, params[i]);
			}
			rs = ps.executeQuery();
			while (rs.next()){
				T obj = clz.newInstance();
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
				PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors();
				for (PropertyDescriptor pd : pds){
					String name = pd.getName();
					Object value = rs.getObject(name);
					Method setter = pd.getWriteMethod();
					setter.invoke(obj, value);
				}
				list.add(obj);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return list;
	}

	public <T> int update( Object obj){
		StringBuffer sb = new StringBuffer();
		StringBuffer elems = new StringBuffer();
		Class clz = obj.getClass();
		if (clz == TaskDetail.class) {
			sb.append("update t_tasks set ");
		} else {
			sb.append("update t_delay set ");
		}		
		Connection conn = null;
		PreparedStatement ps = null;
		String taskId = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(clz, Object.class);
			PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor pd : pds){
				String name = pd.getName();
				System.out.println(name);
				Method getter = pd.getReadMethod();
				Object type = pd.getPropertyType();
				try {
					Object value = getter.invoke(obj);
					if (name.equals("taskId")){
						taskId = value.toString();
					} else if (value != null) {
						if (type == String.class || type == java.util.Date.class || type == java.sql.Date.class){
							elems.append(name+" = '"+value.toString()+"', ");	
						} else {
							elems.append(name+" = "+value.toString()+", ");	
						}										
					}	
				} catch (IllegalArgumentException e) {
					System.out.println("null");
				}									
			}
			elems.replace(elems.length()-2, elems.length()," where taskId = "+taskId);
			sb.append(elems);			
			String sql = sb.toString();
			System.out.println(sql);
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			return ps.executeUpdate(); 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, null);
		}
		return 0;
		
	}
	
	public int delete(String sql){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			return ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, null);
		}
		return 0;
		
	}
	
	public <T> int insert(Object obj) throws MySQLSyntaxErrorException{
		StringBuffer sb = new StringBuffer();
		StringBuffer beans = new StringBuffer();
		StringBuffer elems = new StringBuffer();
		Class clz = obj.getClass();
		if (clz == TaskDetail.class) {
			sb.append("insert into t_tasks ");
		} else {
			sb.append("insert into t_delay ");
		}			
		beans.append("(");
		elems.append("(");
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(clz, Object.class);
			PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor pd : pds){
				String name = pd.getName();
				Method getter = pd.getReadMethod();
				try {
					Object value = getter.invoke(obj);
					if (value != null && value.toString().trim().length() != 0) {
						beans.append(name+", ");
						if (value.getClass() == String.class || value.getClass() == java.util.Date.class || value.getClass() == java.sql.Date.class){
							elems.append("'"+value.toString()+"', ");	
						} else {
							elems.append(value.toString()+", ");	
						}
					}
				} catch (IllegalArgumentException e) {
					System.out.println("null");
				}		
			}
			beans.replace(beans.length()-2, beans.length(), ")");
			elems.replace(elems.length()-2, elems.length(), ")");
			sb.append(beans);
			sb.append(" values ");
			sb.append(elems);
			String sql = sb.toString();
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			return ps.executeUpdate(); 
			
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return 0;
	
	}
	
	public <T> T getRow(Class<T> clz, String sql) {
		T obj = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = DBUtil.getConn();
			obj = clz.newInstance();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
				PropertyDescriptor [] pds = beanInfo.getPropertyDescriptors();
				for (PropertyDescriptor pd : pds){
					String name = pd.getName();
					Object value = rs.getObject(name);
					Method setter = pd.getWriteMethod();
					setter.invoke(obj, value);
				}
			}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return obj;
	}

}
